package com.zoke.support.view;

import android.content.Context;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.zoke.support.R;

public class ZokeLinearLayout extends LinearLayout {
	public ZokeLinearLayout(Context context) {
		super(context);
	}

	public ZokeLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public void setBackgroundDrawable(Drawable d) {
		ZokeLinearLayoutBackgroundDrawable layer = new ZokeLinearLayoutBackgroundDrawable(
				d);
		super.setBackgroundDrawable(layer);
	}

	@Override
	public boolean isInEditMode() {
		return true;
	}

	protected class ZokeLinearLayoutBackgroundDrawable extends LayerDrawable {

		protected ColorFilter _pressedFilter = new LightingColorFilter(
				getContext().getResources().getColor(R.color.pressColor), 1);
		protected int _disabledAlpha = 100;

		public ZokeLinearLayoutBackgroundDrawable(Drawable d) {
			super(new Drawable[] { d });
		}

		@Override
		protected boolean onStateChange(int[] states) {
			boolean enabled = false;
			boolean pressed = false;

			for (int state : states) {
				if (state == android.R.attr.state_enabled)
					enabled = true;
				else if (state == android.R.attr.state_pressed)
					pressed = true;
			}

			mutate();
			if (enabled && pressed) {
				setColorFilter(_pressedFilter);
			} else if (!enabled) {
				setColorFilter(null);
				setAlpha(_disabledAlpha);
			} else {
				setColorFilter(null);
			}

			invalidateSelf();

			return super.onStateChange(states);
		}

		@Override
		public boolean isStateful() {
			return true;
		}
	}
}
